package com.allwees.bs.c.module.user.repository;

import com.allwees.bs.c.module.user.entity.MessageEntity;
import com.allwees.bs.core.model.user.entity.MessageModel;
import com.allwees.core.common.util.reflect.PojoUtil;
import com.allwees.sender.message.MessageSaveDao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MessageRepository extends JpaRepository<MessageEntity, Integer>, MessageSaveDao {

    @Query(value = "SELECT e FROM MessageEntity e WHERE e.userUuid = :userUuid and e.isDeleted = 0 order by e.id desc")
    Page<MessageEntity> findByPage(@Param("userUuid") String userUuid, Pageable pageable);

    MessageEntity getByUuid(String messageUuid);

    @Override
    default void saveMessage(MessageModel model) {
        save(PojoUtil.copy(new MessageEntity(), model));
    }

    List<MessageEntity> findAllByUserUuid(String userUuid);
}
